package com.pesking.core.sqlparser.processor;

import java.util.*;

import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;
import com.pesking.core.util.StringUtils;

public class InDigitSqlParameterHandler extends SqlParameterProcessor {
	@Override
	public String getTitle() {
		return "IN语句参数";
	}

	@Override
	public String getDescription() {
		return this.getTitle() + " 生成数字型的IN语句,比如：(1,2,3,4)";
	}

	@Override
	public void handle(StringBuilder sbSQL, Parameter parameter, List<SqlParameter> processors,SqlParameter sqlParameter) {
		String name = sqlParameter.getName();
		String []values = parameter.getParameterValues(name);
		if (values == null || values.length == 0){
			sbSQL.append("(null)");
		}
		else{
			int len = values.length;
			boolean first = true;
			Set<String> set = new HashSet<>();
			for(int i = 0 ; i < len ; i++){
				set.add(values[i]);
			}
            for (String v : set) {
                if (!StringUtils.isBlank(v)) {
                    v = formatValue(v);
                    if (first) {
                        first = false;
                        sbSQL.append("(").append(v);
                    } else {
                        sbSQL.append(",").append(v);
                    }
                }
            }
			
			if (!first){
				sbSQL.append(")");
			}
			else{
				sbSQL.append("(null)");
			}
		}
	}

	@Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"IN_D"});
	}
}
